<?php
	session_start();
	header('content-type:text/html;charset=utf8');
//print_r($_POST);

//注册之前要验证注册码与是否是合法提交过来的
if(!empty($_POST['toreg']) && $_POST['toreg'] == '注 册'){
	if($_POST['ucode'] == $_SESSION['code']){
		//如果验证码正确了,则开始注册
		try{
			$link = new PDO('mysql:host=localhost;port=3306;dbname=kp5b_blog','root','root');
			$link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
		}catch(PDOException $e){
			die('数据库连接出错:'.$e->getMessage());
		}
		
		//把表单提交过来的数据写入数据库
		/*Array ( [uname] => hdm 
		[upwd] => 123456 
		[upwd1] => 123456 
		[uface] => ./img/faces/AvengersAgeofUltron006.jpg 
		[uemail] => huodeming1@163.com 
		[uhelp] => 我是谁? 
		[ureturn] => 我 
		[ucode] => ra59 
		[toreg] => 注 册 )*/
		//在注册之前要查询用户名有没有,有则不注册
		$sql = 'select uid from users where uname=?';
		try{
			$stmt = $link->prepare($sql);
			$stmt->execute([$_POST['uname']]);
			if($stmt->rowCount()>0){
				die('用户名已存在,请重新填写表单!<a href="reg.php">返回</a>');
			}
		}catch(PDOException $e){
			die('查询用户是否存在时出错:'.$e->getMessage().'<a href="reg.php">返回</a>');
		}
	
		//进行注册
		$sql = 'insert into users(uname,upwd,uemail,uface,uhelp,ureturn) values(?,?,?,?,?,?)';
		try{
			$stmt = $link->prepare($sql);
			$stmt->execute([$_POST['uname'],md5($_POST['upwd']),$_POST['uemail'],$_POST['uface'],$_POST['uhelp'],$_POST['ureturn']]);
			echo '注册成功!';
		}catch(PDOException $e){
			die('写入数据库出错:'.$e->getMessage().'<a href="reg.php">返回</a>');
		}
	}
}
	
	
	//===验证码 start=====================================================================
	$code_str = 'abcdefghijkmnpqrstuvwxyz23456789';//规定验证码由哪些字符串组成
	$max = strlen($code_str)-1;//得到字符串的长度
	$code = '';
	$code .= $code_str[mt_rand(0, $max)];//取得验证码第一位
	$code .= $code_str[mt_rand(0, $max)];//取得验证码第一位
	$code .= $code_str[mt_rand(0, $max)];//取得验证码第一位
	$code .= $code_str[mt_rand(0, $max)];//取得验证码第一位
	//生成验证码第一步是要把验证码写入session
	$_SESSION['code'] = $code;
	//echo $code;
	//=====验证码 end===================================================================
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>多用户博客</title>
		<link rel="stylesheet" type="text/css" href="css/index.css"/>
	</head>

	<body>
		<?php include_once 'header.php';  ?>
		<div id="body">
			<div id="reg">
				<form id="freg" action="reg.php" method="post" onsubmit="return goreg();">
				<form action="reg.php" method="post">
					<table border="0" cellspacing="10" cellpadding="0">
						<!--<colgroup>
							<col width="100" align="right"/>
							<col width="500"/>
						</colgroup>-->
						
						<tr>
							<td class="right">用户名:</td>
							<td><input placeholder="请输入2位以上用户名" type="text" name="uname" value="" /></td>
						</tr>
						<tr>
							<td class="right">密码:</td>
							<td><input placeholder="请输入6位以上密码" type="password" name="upwd" value="" /></td>
						</tr>
						<tr>
							<td class="right">确认密码:</td>
							<td><input placeholder="请再次输入密码" type="password" name="upwd1" value="" /></td>
						</tr>
						<tr>
							<td class="right">用户头像:</td>
							<td>
								<select name="uface" onchange="face_select_img.src = this.value;">
									<?php
									$path = './img/faces';
									$dir = opendir($path);
									while($filename = readdir($dir)){
										if($filename != '.' && $filename != '..'){
											echo '<option value="'.$path.'/'.$filename.'">'.$filename.'</option>';
										}
									}
									?>
								</select>
								<!--<br />-->
								<img id="face_select_img" src="img/faces/001.jpg" width="80" height="80"/>
								
							</td>
						</tr>
						<tr>
							<td class="right">电子邮箱:</td>
							<td><input placeholder="请输入正确电子邮箱" type="text" name="uemail" value="" /></td>
						</tr>
						<tr>
							<td class="right">密保问题:</td>
							<td><input placeholder="请输入密保问题" type="text" name="uhelp" value="" /></td>
						</tr>
						<tr>
							<td class="right">密保答案:</td>
							<td><input placeholder="请输入密保答案" type="text" name="ureturn" value="" /></td>
						</tr>
						<tr>

							<td class="right">验证码:</td>
							<td>
								<input type="text" name="ucode" value="" style="width: 60px;" />
								<span id="code1"><?php  echo $_SESSION['code']; ?></span>
								
								</td>
						</tr>
						<tr>
							<td class="right"></td>
							<td>
								<input type="reset" value="重 填"/>
								<input type="submit" name="toreg" value="注 册"/>
							</td>
						</tr>
					</table>
	
					
				</form>
			</div>
		
		</div>
			<!--<embed type="application/x-shockwave-flash" src="img/hamster.swf" width="280" height="210" id="flashID" name="flashID" bgcolor="#ccc" quality="high" flashvars="up_backgroundColor=FFFFFF" wmode="opaque" allowscriptaccess="always">-->
		<!--网站底部,写好后提取到单独页面-->
		<?php include 'bottom.php';  ?>
		
	</body>
</html>
